#include "muduo/proto/codec.h"
#include "muduo/proto/dispatcher.h"
#include "muduo/base/Logging.h"
#include "muduo/base/Mutex.h"
#include "muduo/net/EventLoopThread.h"
#include "muduo/net/TcpClient.h"

#include "request.pb.h"
#include <iostream>
#include <unordered_map>

class Client
{
public:
    typedef std::shared_ptr<google::protobuf::Message> MessagePtr;
    typedef std::shared_ptr<pxt::TranslateResponse> TranslateResponsePtr;
    typedef std::shared_ptr<pxt::AddResponse> AddResponsePtr;
    Client(std::string ip, int port)
        :_latch(1)
        ,_client(_loopthread.startLoop(), muduo::net::InetAddress(ip, port), "Client")
        ,_dispatcher(std::bind(&Client::onUnknowMessage, this, std::placeholders::_1,
            std::placeholders::_2, std::placeholders::_3))
        ,_codec(std::bind(&ProtobufDispatcher::onProtobufMessage, &_dispatcher, std::placeholders::_1,
            std::placeholders::_2, std::placeholders::_3))
    {
        // 注册业务请求处理函数
        _dispatcher.registerMessageCallback<pxt::TranslateResponse>(std::bind(&Client::onTranslate, this, 
            std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
        _dispatcher.registerMessageCallback<pxt::AddResponse>(std::bind(&Client::onAdd, this, 
            std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
        _client.setConnectionCallback(std::bind(&Client::onConnection, this, std::placeholders::_1));
        _client.setMessageCallback(std::bind(&ProtobufCodec::onMessage, &_codec, std::placeholders::_1,
            std::placeholders::_2, std::placeholders::_3));
    }
    void Add(int num1, int num2)
    {
        pxt::AddRequest req;
        req.set_num1(num1);
        req.set_num2(num2);
        send(&req);
    }
    void Translate(const std::string &msg)
    {
        pxt::TranslateRequest req;
        req.set_msg(msg);
        send(&req);
    }
    void connect()
    {
        _client.connect();
        _latch.wait();
    }
private:
    bool send(const google::protobuf::Message *msg)
    {
        if(_conn->connected())
        {
            _codec.send(_conn, *msg);
            return true;
        }
        return false;
    }
    void onUnknowMessage(const muduo::net::TcpConnectionPtr &conn, const MessagePtr &message, muduo::Timestamp)
    {
        LOG_INFO << "onUnknowMessage" << message->GetTypeName();
        conn->shutdown();
    }
    void onTranslate(const muduo::net::TcpConnectionPtr &conn, const TranslateResponsePtr &message, muduo::Timestamp)
    {
        std::cout << "翻译结果: " << message->msg() << std::endl;
    }
    void onAdd(const muduo::net::TcpConnectionPtr &conn, const AddResponsePtr &message, muduo::Timestamp)
    {
        std::cout << "加法结果: " << message->result() << std::endl;

    }
    void onConnection(const muduo::net::TcpConnectionPtr &conn)
    {
        if(conn->connected())
        {
            _latch.countDown();
            _conn = conn;
        }
        else
        {
            _conn.reset();
        }
    }
private:
    muduo::net::EventLoopThread _loopthread;
    muduo::CountDownLatch _latch;
    muduo::net::TcpConnectionPtr _conn;
    muduo::net::TcpClient _client; // 服务器对象
    ProtobufDispatcher _dispatcher; // 请求分发器对象 -- 注册处理函数
    ProtobufCodec _codec; // protobuf协议处理器 -- 针对收到的请求数据进行protobuf协议处理
};

int main()
{
    Client client("127.0.0.1", 8080);
    
    client.connect();
    
    client.Add(100,200);

    client.Translate("hello");

    sleep(1);

    return 0;
}